﻿using System;
using System.Collections.Generic;

namespace SmartMotion.Core
{
    public class MotionMgr
    {

        #region 常用功能

        //Init
        //AddCard
        //DeleCard
        //ModifyCard
        //getCardInfo
        //GetAllCard
        //GetAllCardName

        #endregion
        #region 单例

        private static readonly Lazy<MotionMgr> Instance = new Lazy<MotionMgr>(() => new MotionMgr());

        private MotionMgr()
        {
            CardFilePath = GlobalVars.Ins.GetConfigPath() + "MotionMgr.cfg";
            _cards = new List<MotionControlBase>();
        }

        public static MotionMgr Ins { get; } = Instance.Value;

        #endregion 单例

        public static readonly string MotionCard = "MotionCard";

        private readonly List<MotionControlBase> _cards;

        public string CardFilePath { get; set; }

        public List<MotionControlBase> Cards
        {
            get
            {
                foreach (var varInfo in GlobalVars.Ins.Vars[GlobalVars.MotionMgr].Values)
                {
                    if (varInfo.Group != MotionCard) continue;
                    if (!_cards.Contains(varInfo.Value))
                    {
                        _cards.Add(varInfo.Value);
                    }
                }

                return _cards;
            }
        }

        public void Load()
        {
            GlobalVars.Ins.LoadDic(GlobalVars.MotionMgr, CardFilePath, false);
            //把轴引用从变量里拿到给卡
            foreach (var moitonCardBase in Cards)
            {
                for (int j = 0; j < moitonCardBase.AxisesID.Count; j++)
                {
                    moitonCardBase.Axises.Add(GlobalVars.Ins.GetVar(moitonCardBase.AxisesID[j]).Value);
                }

                for (int j = 0; j < moitonCardBase.DIID.Count; j++)
                {
                    VarInfo varInfo = GlobalVars.Ins.GetVar(moitonCardBase.DIID[j]);
                    if (varInfo != null) moitonCardBase.DI.Add(varInfo.Value);
                }

                for (int j = 0; j < moitonCardBase.DOID.Count; j++)
                {
                    VarInfo varInfo = GlobalVars.Ins.GetVar(moitonCardBase.DOID[j]);
                    if (varInfo != null) moitonCardBase.DO.Add(varInfo.Value);
                }

            }
        }

        public void Save()
        {
            GlobalVars.Ins.SaveDic(GlobalVars.MotionMgr, CardFilePath);
        }

    
    }
}